package cn.iocoder.yudao.module.self.mq.service;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;

@Service
public class MyRabbitMqConsumerService {
    @Autowired
    RabbitTemplate rabbitTemplate;

    public String consumeMessage(){
//        Message receive = rabbitTemplate.receive("TestDirectQueue");
//        try {
//            System.out.println(new String(receive.getBody(),"utf-8"));
//        } catch (UnsupportedEncodingException e) {
//            throw new RuntimeException(e);
//        }
        Object o = rabbitTemplate.receiveAndConvert("TestDirectQueue");
        System.out.println(o);
        return "consume ok!";
    }


    public String consumeTopicMessage1(){
        Object o = rabbitTemplate.receiveAndConvert("topic.man");
        System.out.println("当前方法：consumeTopicMessage1===="+o);
        return "consume ok!";
    }

    public String consumeTopicMessage2(){
        Object o = rabbitTemplate.receiveAndConvert("topic.woman");
        while(o!= null){
            System.out.println("当前方法：consumeTopicMessage2===="+o);
            o = rabbitTemplate.receiveAndConvert("topic.woman");
        }
        System.out.println("当前方法：consumeTopicMessage3===="+o);
        return "consume ok!";
    }

    public String consumeTopicMessage3(){
        Object o1 = rabbitTemplate.receiveAndConvert("topic.woman.other");
        while(o1 != null){
            System.out.println("当前方法：consumeTopicMessage3===="+o1);
            o1 = rabbitTemplate.receiveAndConvert("topic.woman.other");

        }
        System.out.println("当前方法：consumeTopicMessage3===="+o1);

        return "consume ok!";
    }
}
